package com.etc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.etc.entity.Equipment;
import com.etc.entity.RoleEquipment;
import com.etc.tools.DBTools;

public class RoleEquipmentDAO {

	DBTools tool = new DBTools();
	Connection conn = tool.getConnection();
	EquipmentDAO equipDao = new EquipmentDAO();
	//添加角色装备
	public void addRoleEquip(RoleEquipment roleEquip) {
		String sql = "insert into T_ROLE_EQUIPMENT values(?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, roleEquip.getReId());
			ps.setString(2, roleEquip.getMainWeap().getEquipId());
			ps.setString(3, roleEquip.getSecondWeap().getEquipId());
			ps.setString(4, roleEquip.getHelmet().getEquipId());
			ps.setString(5, roleEquip.getTop().getEquipId());
			ps.setString(6, roleEquip.getBottom().getEquipId());
			ps.setString(7, roleEquip.getShoe().getEquipId());
			int confrim = ps.executeUpdate();
			if(confrim==1) System.out.println("角色装备添加成功");
		} catch (SQLException e) {
			System.out.println("角色装备查询异常");
			e.printStackTrace();
		}
	}
	//根据角色装备id查询角色装备
	public RoleEquipment findREById(String reId) {
		String sql = "select * from T_ROLE_EQUIPMENT where re_id = ?";
		PreparedStatement ps = null;
		RoleEquipment re = null;
		Equipment mainWeap = null;
		Equipment secondWeap = null;
		Equipment helmet = null;
		Equipment top = null;
		Equipment bottom = null;
		Equipment shoe = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, reId);
			ResultSet set = ps.executeQuery();
			set.next();
			mainWeap =equipDao.findEquipById(set.getString("main_id"));
			secondWeap =equipDao.findEquipById(set.getString("second_id"));
			helmet =equipDao.findEquipById(set.getString("helmet_id"));
			top =equipDao.findEquipById(set.getString("top_id"));
			bottom =equipDao.findEquipById(set.getString("bottom_id"));
			shoe =equipDao.findEquipById(set.getString("shoe_id"));
			re = new RoleEquipment(set.getString("re_id"), mainWeap, secondWeap, helmet, top, bottom, shoe);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return re;
	}
}
